package cn.kgc.springday03.factory;

import com.mysql.jdbc.Driver;
import org.springframework.beans.factory.FactoryBean;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author 课工场
 * @date 2023/10/8
 * @description 使用工厂方式 获取复杂类型对象   Connection
 * <p>
 * 类一旦实现了FactoryBean 接口  在spring容器中获取对象时 得到是FactoryBean 泛型类型的对象  getObject()
 */
public class ConnectionFactoryBean implements FactoryBean<Connection> {

    @Override
    public Connection getObject() throws Exception {

        Driver driver = new Driver();
        DriverManager.registerDriver(driver);
        String username = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/java2322?useSSL=false";
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;

    }

    @Override
    public Class<?> getObjectType() {
        return Connection.class;
    }
}
